<template>
  <el-table
    style="margin: 20px 0"
    border
    stripe
    row-key="id"
    :data="permissionArr"
  >
    <el-table-column label="名称" align="center" prop="name"></el-table-column>
    <el-table-column
      label="权限值"
      align="center"
      prop="code"
    ></el-table-column>
    <el-table-column
      label="修改时间"
      align="center"
      prop="updateTime"
    ></el-table-column>
    <el-table-column label="操作" align="center" width="300px">
      <template #="{ row, $index }">
        <el-button
          type="success"
          size="small"
          icon="Plus"
          :disabled="row.level === 4"
          @click="onAddPermission(row)"
        >
          {{ row.level === 3 ? '添加功能' : '添加菜单' }}
        </el-button>
        <el-button
          type="primary"
          size="small"
          icon="Edit"
          :disabled="row.level === 1"
          @click="onEdit(row)"
        >
          编辑
        </el-button>
        <el-popconfirm :title="`你确定要删除${row.name}?`" width="300px" @confirm="onDelete(row.id)">
          <template #reference>
            <el-button
              type="danger"
              size="small"
              icon="Delete"
              :disabled="row.level === 1"
            >
              删除
            </el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="dialogVisible" :title="menuData.id?'更新菜单':'添加菜单'">
    <el-form>
      <el-form-item label="名称">
        <el-input placeholder="请输入菜单名称" v-model="menuData.name"/>
      </el-form-item>
      <el-form-item label="权限">
        <el-input placeholder="请输入权限数值" v-model="menuData.code"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="onSave">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import {onMounted, reactive, ref} from 'vue'
import type {
  MenuParams,
  Permission,
  PermissionList,
  PermissionResponseData,
} from '@/api/acl/menu/type'
import {reqAddOrUpdateMenu, reqAllPermission, reqRemoveMenu} from '@/api/acl/menu'
import {ElMessage} from "element-plus";

// 存储菜单数据
let permissionArr = ref<PermissionList>([])
// 对话框显示、隐藏
let dialogVisible = ref<boolean>(false)
// 携带的参数
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0
})

onMounted(() => {
  getHasPermission()
})
// 获取菜单数据
const getHasPermission = async () => {
  let result: PermissionResponseData = await reqAllPermission()
  if (result.code === 200) {
    permissionArr.value = result.data
  }
}
// 点击添加菜单
const onAddPermission = (row: Permission) => {
  // 清空数据
  Object.assign(menuData, {
    id: 0,
    code: '',
    level: 0,
    name: '',
    pid: 0
  })
  dialogVisible.value = true
  menuData.level = row.level + 1
  menuData.pid = row.id
}
// 点击编辑
const onEdit = (row: Permission) => {
  dialogVisible.value = true
  Object.assign(menuData, row)
}
// 点击删除
const onDelete = async (id: number) => {
  let result = await reqRemoveMenu(id)
  if (result.code === 200) {
    ElMessage({type: "success", message: '删除成功'})
    await getHasPermission()
  }
}
// 点击确定
const onSave = async () => {
  let result = await reqAddOrUpdateMenu(menuData)
  if (result.code === 200) {
    dialogVisible.value = false
    ElMessage({
      type: "success",
      message: menuData.id ? '更新成功' : '添加成功'
    })
    await getHasPermission()
  } else {
    dialogVisible.value = false
    ElMessage({type: "error", message: result.data})
  }
}
</script>
<style scoped lang="scss"></style>
